package com.dabai.iot.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dabai.common.core.domain.R;
import com.dabai.common.core.utils.Func;
import com.dabai.common.mybatis.core.page.PageQuery;
import com.dabai.common.mybatis.core.page.TableDataInfo;
import com.dabai.common.web.core.BaseController;
import com.dabai.iot.core.convert.ThingModelBlockConvert;
import com.dabai.iot.core.dto.ThingModelBlockInput;
import com.dabai.iot.core.dto.ThingModelBlockUpdate;
import com.dabai.iot.core.entity.ThingModelBlockEntity;
import com.dabai.iot.core.service.IThingModelBlockService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 物模型功能模块 控制器
 *
 * @author kev1n
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/thingModel/block")
public class ThingModelBlockController extends BaseController {

    private final IThingModelBlockService thingModelBlockService;

    /**
     * 新增物模型功能模块
     */
    @PostMapping("/add")
    @Operation(summary = "新增物模型功能模块")
    public R<Void> add(@Valid @RequestBody ThingModelBlockInput blockInput) {
        ThingModelBlockEntity modelBlock = Objects.requireNonNull(ThingModelBlockConvert.INSTANCE.convert(blockInput));
        return toAjax(thingModelBlockService.addModelBlock(modelBlock));
    }

    /**
     * 修改物模型功能模块
     */
    @PostMapping("/update")
    @Operation(summary = "修改物模型功能模块")
    public R<Void> update(@Valid @RequestBody ThingModelBlockUpdate blockUpdate) {
        return toAjax(thingModelBlockService.updateModelBlock(blockUpdate));
    }

    /**
     * 删除物模型功能模块
     */
    @PostMapping("/remove")
    @Operation(summary = "删除物模型功能模块", description = "传入主键id")
    public R<Void> remove(@Parameter(description = "主键id", required = true) @RequestParam Long id) {
        return toAjax(thingModelBlockService.removeModelBlock(id));
    }

    /**
     * 物模型功能模块详情
     */
    @GetMapping("/detail")
    @Operation(summary = "物模型功能模块详情", description = "传入主键id")
    public R<ThingModelBlockEntity> detail(@RequestParam Long id) {
        ThingModelBlockEntity detail = thingModelBlockService.getById(id);
        return R.ok(detail);
    }

    /**
     * 物模型功能模块下拉数据源
     */
    @GetMapping("/select")
    @Operation(summary = "物模型功能模块下拉数据源", description = "用于物模型模块下拉选择，传入产品主键 productId")
    public R<List<ThingModelBlockEntity>> select(@RequestParam Long productId, String blockName) {
        List<ThingModelBlockEntity> selectList = thingModelBlockService.lambdaQuery()
            .eq(ThingModelBlockEntity::getProductId, productId)
            .like(Func.isNotEmpty(blockName), ThingModelBlockEntity::getBlockName, blockName)
            .list();
        return R.ok(selectList);
    }

    /**
     * 物模型功能模块分页
     */
    @GetMapping("/page")
    @Operation(summary = "物模型功能模块分页", description = "传入对应查询参数")
    public TableDataInfo<ThingModelBlockEntity> page(PageQuery pageQuery, ThingModelBlockEntity thingModel) {
        IPage<ThingModelBlockEntity> pages = thingModelBlockService.getModelBlockPage(pageQuery.build(), thingModel);
        return TableDataInfo.build(pages);
    }

}
